//
//  AppDelegate.m
//  Accounting
//
//  Created by Billy on 15/8/5.
//  Copyright (c) 2015年 hzqs. All rights reserved.
//

#import "AppDelegate.h"
#import "RootViewController.h"
#import "OnboardingViewController.h"
#import "OnboardingContentViewController.h"
#import "IQKeyboardManager.h"
#import <AlipaySDK/AlipaySDK.h>

#import "PublishViewController.h"

@interface AppDelegate ()
@property (nonatomic,assign) NSInteger number;
@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent]; //statusBar颜色
    
    
    /** launchTimes:  利用该值来决定是否显示欢迎界面*/
    NSString *launchTimes = [[NSUserDefaults standardUserDefaults] valueForKey:@"launch"];
    
    if (!launchTimes) {
        
        [self.window setRootViewController:[self welcomeViewControllerCreation]];
        [[NSUserDefaults standardUserDefaults] setValue:@"1" forKey:@"launch"];
        
    } else {
        
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:[RootViewController new]];
        [self.window setRootViewController:nav];
    }
    
    // 键盘控制code
    [[IQKeyboardManager sharedManager] setEnable:YES];
    [[IQKeyboardManager sharedManager] setShouldResignOnTouchOutside:YES];
    [[IQKeyboardManager sharedManager] setShouldToolbarUsesTextFieldTintColor:YES];
    [[IQKeyboardManager sharedManager] setEnableAutoToolbar:NO];
    
    [self.window makeKeyAndVisible];
    
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}



#pragma mark - Welcome Page


- (OnboardingViewController *) welcomeViewControllerCreation {
    
    
    OnboardingContentViewController *firstPage = [OnboardingContentViewController
                                                  contentWithTitle:@"first page"
                                                  body:@"say something!"
                                                  image:[UIImage imageNamed:@"blue"]
                                                  buttonText:@"button"
                                                  action:^{
                                                      
                                                      NSLog(@"处理button");
                                                      
                                                  }];
    
    OnboardingContentViewController *secondPage = [OnboardingContentViewController
                                                   contentWithTitle:@"second page"
                                                   body:@"say something!"
                                                   image:[UIImage imageNamed:@"red"]
                                                   buttonText:@"button"
                                                   action:^{
                                                       NSLog(@"处理button");
                                                       
                                                   }];
    
    OnboardingContentViewController *thirdPage = [OnboardingContentViewController
                                                  contentWithTitle:@"third page"
                                                  body:@"say something!"
                                                  image:[UIImage imageNamed:@"yellow"]
                                                  buttonText:@"开始使用"
                                                  action:^{
                                                      UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:[RootViewController new]];
                                                      [[NSUserDefaults standardUserDefaults] setValue:@"1" forKey:@"launch"];
                                                      [self.window setRootViewController:nav];
                                                  }];
    
    
    
    OnboardingViewController *welcomeVC = [OnboardingViewController onboardWithBackgroundImage:[UIImage imageNamed:@"tree"] contents:@[firstPage,secondPage,thirdPage]];
    
    welcomeVC.allowSkipping = YES;
    welcomeVC.skipHandler = ^{
        
        [[NSUserDefaults standardUserDefaults] setValue:@"1" forKey:@"launch"];
        
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:[RootViewController new]];
        
        [self.window setRootViewController:nav];
        
    };
    
    return welcomeVC;
    
}

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    
    if ([url.host isEqualToString:@"safepay"]) {
        
        [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
            
        }];
    }
    
    return  YES;
}

@end
